/*
 * Copyright 2018 Red Hat, Inc. and/or its affiliates.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.aograph.listeners;

import com.aograph.entity.AirlinePredict;
import org.kie.api.event.rule.AfterMatchFiredEvent;
import org.kie.api.event.rule.BeforeMatchFiredEvent;
import org.kie.api.event.rule.DefaultAgendaEventListener;
import org.kie.api.runtime.rule.Match;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AographAgendaEventListener extends DefaultAgendaEventListener {

    private static  Logger logger= LoggerFactory.getLogger(AographAgendaEventListener.class);

    private static Map<String,String> cache=new HashMap<>();

    @Override
    public void beforeMatchFired(BeforeMatchFiredEvent event) {
//        Match matchEvent=event.getMatch();
//        List<Object> objects=matchEvent.getObjects();
//        if (null!=objects && objects.size()>0){
//            for (Object obj:objects){
//                if (obj instanceof AirlinePredict){
//                    String cacheId=((AirlinePredict) obj).getSkey()+event.getMatch().getRule().getId();
//                    cache.put(cacheId,obj.toString());
//                }
//                if (obj instanceof List){
//                    for (AirlinePredict airlinePredict:(List<AirlinePredict>) obj){
//                        String cacheId=airlinePredict.getSkey()+event.getMatch().getRule().getId();
//                        cache.put(cacheId,airlinePredict.toString());
//                    }
//                }
//
//            }
//        }

    }

    @Override
    public void afterMatchFired(AfterMatchFiredEvent event) {
//        Match matchEvent=event.getMatch();
//        List<Object> objects=matchEvent.getObjects();
//        if (null!=objects && objects.size()>0){
//            for (Object obj:objects){
//                if (obj instanceof AirlinePredict) {
//                    String cacheId = ((AirlinePredict) obj).getSkey() + event.getMatch().getRule().getId();
//                    StringBuffer sb = new StringBuffer("ruleName:" + event.getMatch().getRule().getName());
//                    if (cache.containsKey(cacheId)) {
//                        sb.append(",beforeFire:" + cache.get(cacheId));
//                        cache.remove(cacheId);
//                    }
//                    sb.append(",afterFire:" + obj.toString());
//                    logger.info(sb.toString());
//                }
//                if (obj instanceof List){
//                    for (AirlinePredict airlinePredict:(List<AirlinePredict>) obj){
//                        String cacheId = airlinePredict.getSkey() + event.getMatch().getRule().getId();
//                        StringBuffer sb = new StringBuffer("ruleName:" + event.getMatch().getRule().getName());
//                        if (cache.containsKey(cacheId)) {
//                            sb.append(",beforeFire:" + cache.get(cacheId));
//                            cache.remove(cacheId);
//                        }
//                        sb.append(",afterFire:" + airlinePredict.toString());
//                        logger.info(sb.toString());
//                    }
//                }
//
//            }
//        }

    }

}
